package com.huanglj.himalaya.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.huanglj.himalaya.R;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.InnerHolder> {

    private static final String TAG = "AlbumAdapter";
    List<Album> mAlbumList = new ArrayList<>();
    private OnRecommendItemClickListener mListener;
    private OnLongClickListerer mLongListener;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载布局
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.recommend_item, parent, false);
        InnerHolder innerHolder = new InnerHolder(inflate);
        return innerHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {
        final View itemView = holder.itemView;
        //方便后面获取tag，来判断取第几个值
        itemView.setTag(position);
        holder.setData(mAlbumList.get(position));
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick((Integer)v.getTag(),mAlbumList.get(position));
                }

            }
        });
        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mLongListener.onLongClick(mAlbumList.get(position));
                return true;
            }
        });


    }

    @Override
    public int getItemCount() {
        return mAlbumList.size();
    }

    public void setData(List<Album> albumList) {
        assert mAlbumList != null;
        mAlbumList.clear();
        mAlbumList.addAll(albumList);
        notifyDataSetChanged();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(@NotNull Album album) {
            TextView albumTitle = itemView.findViewById(R.id.album_title);
            TextView albumIntro = itemView.findViewById(R.id.album_Intro);
            TextView albumPlayCount = itemView.findViewById(R.id.album_play_count);
            TextView albumConstantSize = itemView.findViewById(R.id.album_constant_size);
            ImageView coverSmallImg = itemView.findViewById(R.id.cover_small_img);
            //改变播放显示

            //ui设置数据
            albumTitle.setText(album.getAlbumTitle());
            albumIntro.setText(album.getAlbumIntro());
            albumPlayCount.setText(String.valueOf(album.getPlayCount()));
            albumConstantSize.setText(String.valueOf(album.getIncludeTrackCount()));
            Glide.with(itemView.getContext()).load(album.getCoverUrlLarge()).into(coverSmallImg);
        }
    }

    public void setOnRecommendItemClickListener(OnRecommendItemClickListener listener){
        mListener = listener;
    }
    public interface OnRecommendItemClickListener{
        void onItemClick(Integer tag, Album album);
    }

    public void setOnLongClickListerer(OnLongClickListerer listerer){
        mLongListener = listerer;
    }
    public interface OnLongClickListerer{
        void onLongClick(Album album);
    }
}
